<template>
  <el-card shadow="never" class="rd-8 mb-8">
    <template #header>
      <div class="card-header f-c-c">{{ showMsg }}表单</div>
    </template>
    <el-form style="width: 80%" :model="hotelForm" ref="formRef" :rules="rules">
      <el-form-item label="名称" label-width="100px" prop="name">
        <el-input placeholder="请输入名称" v-model="hotelForm.name"></el-input>
      </el-form-item>
      <el-form-item label="价格" label-width="100px" prop="price">
        <el-input-number
          v-model="hotelForm.price"
          :precision="2"
          :step="0.1"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="评分" label-width="100px" prop="star">
        <el-rate v-model="hotelForm.star" allow-half />
      </el-form-item>
      <el-form-item label="图片" label-width="100px" prop="avatar">
        <CropperImage v-model="hotelForm.image" />
      </el-form-item>
      <el-form-item label="联系方式" label-width="100px" prop="mobile">
        <el-input
          placeholder="请输入位置"
          v-model="hotelForm.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item label="位置" label-width="100px" prop="location">
        <el-input
          placeholder="请输入位置"
          v-model="hotelForm.location"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" label-width="100px" prop="remark">
        <el-input
          placeholder="请输入备注"
          v-model="hotelForm.remark"
          autosize
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item label-width="100px">
        <el-button type="primary" size="default" @click="handleSubmit">
          提交
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { reqUpdateOrAdd, reqGetById } from '@/api/hotel'
import CropperImage from '@/components/CropperImage/index.vue'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
let route = useRoute()
let router = useRouter()

const getInfoById = async (id) => {
  let result = await reqGetById(id)
  hotelForm.value = result.data
}

let hotelForm = ref({})
let formRef = ref()

let showMsg = computed(() => (hotelForm.value.hid ? '编辑' : '添加'))

const rules = {
  name: [{ required: true, message: '名称必须填写' }],
  price: [{ required: true, message: '价格必须填写' }],
  location: [{ required: true, message: '位置必须填写' }],
}

const handleSubmit = async () => {
  await formRef.value.validate(async (valid) => {
    if (valid) {
      await reqUpdateOrAdd(hotelForm.value)
      ElMessage.success(`${showMsg.value}成功`)
      hotelForm.value = {}
      formRef.value.resetFields()
      router.push('/hotel')
    }
  })
}

const init = () => {
  if (route.params && route.params.id) {
    const id = route.params.id
    getInfoById(id) //调用根据 id查询的方法
  } else {
    hotelForm.value = {}
  }
}
watch(
  () => route.params.id,
  () => init(),
  { immediate: true },
)
</script>

<style lang="scss" scoped>
.card-header {
  font-weight: 600;
  color: #3e454d;
  padding: 4px 13px 4px 12px;
}
</style>
